$(function(){
    console.log("totp");

    var host = window.location.host;
    console.log(host);

    var tokenInput = document.getElementsByName('token');
    if(tokenInput && tokenInput.length>0){
        if(host == 'sandbox.open.10086.cn'){
            showTostInfo('dingkekui', '');
            GenerateTOTP("FUO7O6YQUBW65R7M", 'ft_token');
            ConfigureHandlers();
        }else{
            showTostInfo('testuser', '');
            GenerateTOTP("IHV6YAG56ULK2IO6", 'ft_token');
            ConfigureHandlers();
        }
    }
})
var copyFlag = false;

$(document).on('click', 'input[id="ft_token"]', function(event) {
    console.log('ft_token');
    window.copyFlag = true;
});
$(document).on('click', 'input[name="token"]', function(event) {
    console.log(copyFlag)
    if(window.copyFlag){
        // document.getElementsByName("token")[0].value = document.getElementById("ft_token").value;
        const token = document.getElementById("ft_token").value;
        event.currentTarget.value = token;
        for(var i=0; i<token.length; i++){
            dispatchEventCustum(event.currentTarget, parseInt(token[i]));
        }
    }
    window.copyFlag = false;
});

function showTostInfo(name, pass){
    $("body").append('<div style="display: block;position: fixed;background: #fafafae0;top: 120px;padding: 0;font-size: 14px;right: 10px;"><ul class="inf_list clearfix"style="padding-left: 5px;"><li class="clearfix">账户：<input id="ft_name"value="'+name+'"style="padding: 0;border: none;outline: none;background: #fafafae0;padding-left: 5px;width: 100px;"></li><li class="clearfix">密码：<input id="ft_pass"value="'+pass+'"style="padding: 0;border: none;outline: none;background: #fafafae0;padding-left: 5px;width: 100px;"></li><li class="clearfix">令牌：<input id="ft_token"value=""style="padding: 0;border: none;outline: none;background: #fafafae0;padding-left: 5px;width: 100px;"></li></ul><div class="archive-bar"></div></div>');
}


function dispatchEventCustum(that, code){
    console.log('input code:'+code);
    let keyboardEvent = document.createEvent("KeyboardEvent");
    let initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?"initKeyboardEvent" :"initKeyEvent";
    let keyCode= 48 + code;

    keyboardEvent[initMethod](
    "keydown", // event type: keydown, keyup, keypress
    true,      // bubbles
    true,      // cancelable
    window,    // view: should be window
    false,     // ctrlKey
    false,     // altKey
    false,     // shiftKey
    false,     // metaKey
    keyCode,        // keyCode: unsigned long - the virtual key code, else 0
    code          // charCode: unsigned long - the Unicode character associated with the depressed key, else 0
    );
    that.dispatchEvent(keyboardEvent);

    let evt = new InputEvent('input', {
        inputType: 'insertText',
        data: code,
        dataTransfer: null,
        isComposing: false
    });
    that.dispatchEvent(evt);

    evt = document.createEvent("HTMLEvents");
	evt.initEvent("change", true, true);
    that.dispatchEvent(evt);

    evt = document.createEvent("HTMLEvents");
	evt.initEvent("focusout", false, true);
    
}